﻿using BlogManager.Models;
using BlogManager.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace BlogManager.Controllers
{
    public class ComentariosController : ApiController
    {
        private IComentarioRepository repository;

        public ComentariosController()
        {
            repository = new ComentarioRepository();
        }

        public List<Comentario> Get()
        {
            return repository.GetAll();
        }

        public List<Comentario> GetByIdpost(int Idpost)
        {
            return repository.GetByIdpost(Idpost);
        }

        public Comentario Get(int id)
        {
            Comentario item = repository.Get(id);
            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return item;
        }

        public HttpResponseMessage Post(Comentario comentario)
        {
            repository.Add(comentario);

            var response = Request.CreateResponse<Comentario>(System.Net.HttpStatusCode.Created, comentario);

            return response;
        }

        public void Put(int id, Comentario comentario)
        {
            comentario.Id = id;
            if (!repository.Update(comentario))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        public void Delete(int id)
        {
            Comentario item = repository.Get(id);
            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            repository.Remove(id);
        }
    }
}